<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>TXT2M3U</title>
<script>
	function M3ubtnClick() {

		var sourceStr = sourceText.value.split("\n");
		if (sourceStr[0])
			var i = 0;		//计行变量
		var ii = 1;		//序列号变量
		aimText.value = '';		
		//aimText.value += '#EXTM3U\n';
		while (i < sourceStr.length) {			
			if (sourceStr[i].indexOf(",") <= -1){
				var grouptt=sourceStr[i];
			}
			sourceText.value = sourceText.value.replace("$",",");
			if (sourceStr[i].indexOf(",") > -1) {							//如果含有逗号
				var TvUrl = sourceStr[i].split(",");
				if (TvUrl[1].indexOf("#") > -1) {							//含有#号
					var t = TvUrl[1].split("#");
					for (j = 0; j < t.length; j++) {
					aimText.value += '#EXTINF:-1 group-title="' + grouptt +'",' + TvUrl[0] + "\n";
					aimText.value += TvUrl[1] + "\n";										
					++ii;
					}
				} else {													//不含#号
					aimText.value += '#EXTINF:-1 group-title="' + grouptt +'",' + TvUrl[0] + "\n";
					aimText.value += TvUrl[1] + "\n";					
					++ii;
				}
			}
			++i			
		}
	}	

	function dpl2txt() {
		var sourceStr = aimText.value.split("\n");
		sourceText.value = "";
		var i = 0;
		aimFile = "";
		aimName = "";
	}	
</script>
</head>
<body>
	<center>
	<h3>TXT转换M3U列表</h3>
	</center>
<p>
</p>
	<input type="file" id="inputBrow">
	<br>
	<br>
		<select id="encoding">
		<option value="utf-8" selected="">UTF-8</option>	
		<option value="gbk">GBK</option>		
		<option value="gbk">GB2312</option>
		<option value="big5">BIG5</option>
		<option value="korean">EUC-KR（韩文）</option>
		<option value="euc-jp">EUC-JP（日文）</option>
		<option value="utf-16be">UTF-16BE</option>
		<option value="utf-16le">UTF-16LE</option>
	</select>
	<center>
	<br>

<textarea style="height:32%; width:90%;" id="sourceText"></textarea><br><br>
<input type="button" value="转换" onclick="M3ubtnClick()"><br>
<br>
<br><textarea style="height:32%; width:90%;" id="aimText"></textarea><br>

<p>
<input type="button" value="清空" onclick="dpl2txt()"><br>
</p>
</center>

<script>

	function importTxt() {

	}
	// 支持文件拖放
	function dragenter(e) {
		e.stopPropagation();
		e.preventDefault();
	}

	function dragover(e) {
		e.stopPropagation();
		e.preventDefault();
	}

	function drop(e) {
		e.stopPropagation();
		e.preventDefault();
		var dt = e.dataTransfer;
		var files = dt.files;
		if (files.length) {
			var file = files[0];
			var reader = new FileReader();
			reader.onload = function () {
				document.getElementById("sourceText").value = this.result;
			};
			reader.readAsText(file, document.getElementById("encoding").value);
			reader = null;
		}
	}
	var dropbox = document.getElementById("sourceText");
	dropbox.addEventListener("dragenter", dragenter, false);
	dropbox.addEventListener("dragover", dragover, false);
	dropbox.addEventListener("drop", drop, false);

	(function () {
		var input = document.querySelector('#inputBrow');
		var span = document.querySelector('#sourceText');
		input.addEventListener('change', function (e) {
			handFile(e.target.files[0]);
		});

		function handFile(file) {
			console.log('hand');
			var reader = new FileReader();
			reader.onload = function (e) {
				span.value = e.target.result;
			};
			reader.readAsText(file, document.getElementById("encoding").value);
			reader = null;
		}
	})();

</script>
</body></html>